﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SponsorshipManager.Database;
using SponsorshipManager.Domain;


namespace SponsorshipManager.BusinessLayer
{
    public class PersonDTO : IDbBasedObject
    {

        public Person DomainPerson {get; set;}

        public virtual bool Save()
        {
            int ret;

            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            {
                sp.Name = "uspWritePerson";
            }
            sp.AddParameter("Name", DbType.String, DomainPerson.Forename);
            sp.AddParameter("Surame", DbType.String, DomainPerson.Surname);
            sp.AddParameter("Gender", DbType.Int32, DomainPerson.Gender);
            sp.AddParameter("Email", DbType.String, DomainPerson.Email);
            sp.Token = User.CurrentUser.Token;
            ret = sp.ExecuteNoQuery();

            foreach (Address a in DomainPerson.Addresses)
            {
                AddressDTO aDTO = new AddressDTO();
                aDTO.Address = a;
                    aDTO.Save();
    
            }

            foreach (Note n in DomainPerson.Notes) 
            {
                NoteDTO nDTO = new NoteDTO();
                nDTO.Title = n.Title;
                nDTO.Content = n.Content;
                nDTO.Save();
            }


            return true;
        }

        public virtual bool LoadDataRow(DataRow dr, Person P)
        {
            bool ret;
            Guid g;
            string s;
            int i;
            DateTime d;
            try
            {
                s =  dr["PersonGuid"].ToString();
                if (Guid.TryParse(s, out g))
                    P.PrimaryKey = g;
                /*
                s = dr["name"].ToString();;
                if (!String.IsNullOrEmpty(s))
                    P.Name = s;
                */
                s = dr["surname"].ToString();
                if(!String.IsNullOrEmpty(s))
                    P.Surname = s;
                
                s= dr["email"].ToString();
                if(!String.IsNullOrEmpty(s))
                    P.Email = s;

                s = dr["genderId"].ToString();
                if(Int32.TryParse(s,out i))
                    P.Gender = (Person.GenderEnum)i;

                s = dr["DateOfBirth"].ToString();
                if(DateTime.TryParse(s,out d))
                    P.DateOfBirth = d;
                ret = true;
            }
            catch 
            {
                ret = false;
            }
            return ret;
        }

        public virtual bool Load()
        {
                // do something
            bool retval = false;
            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.Name = "uspGetPerson";
            //sp.AddParameter("PersonId", DbType.Int32, PersonId);
            sp.Token = User.CurrentUser.Token;
            int ret = sp.Execute();
            if(ret==1)
            {
                DataRow dr = sp.QueryResult.Rows[0];
                retval = LoadDataRow(dr,DomainPerson);
            }
            return retval;
        }
        



        /*
         * public int Age {
            
            get{

                int DOBDays = DateOfBirth.DayOfYear;
                int TodayDays = DateTime.Today.DayOfYear;
                int DOBYear = DateOfBirth.Year;
                int TodayYear = DateTime.Today.Year;
                int ret = 0;

                int CompareResult = DOBDays.CompareTo(TodayDays);
                switch(CompareResult) {
                    case 0 :
                    case  1 :
                        // Birthday has already happened this year.
                        ret = TodayYear - DOBYear;
                        break;
                    
                    case -1 :
                        // Birthday is still to happen this year.
                        ret = TodayYear - DOBYear - 1;
    
                    break;
                }
                    
                    
                    return ret;

                }

        }
*/
        public static string PersonTypeName(Person p)
        {
            // this will need translating somewhere.
            return p.GetType().ToString();
        }

        public override string ToString() 
        { 
            string nl = Environment.NewLine;
            string s = String.Concat( 
                "Name      : " , DomainPerson.Forename ,                                    nl,
                "Surname   : ", DomainPerson.Surname, nl,
                "Email     : ", DomainPerson.Email, nl,
                "DOB       : ", DomainPerson.DateOfBirth.ToString("dd MMMM yyyy"), nl,
                /*"Gender    : ", DomainPerson.GenderName(DomainPerson.Gender), nl,*/
                "Type      : " , this.GetType().ToString() ,              nl
            );
            return s;
        }

    

        bool IDbBasedObject.NeedsToSave {get; set;}
    }
    }

